<template>
  <div class="charts" ref="chartsRef"></div>
</template>

<script lang="ts">
export default {
  name: "LineChart",
};
</script>

<script lang="ts" setup>
import * as echarts from "echarts";
import { ref, watch } from "vue";
const props = defineProps<{
  data: number[];
  axis: string[];
}>();
// setup相当于beforecreate和created阶段
// 要挂载才有dom元素
const chartsRef = ref();
watch(
  () => props.data,
  () => {
    const myEcharts = echarts.init(chartsRef.value);
    const option = {
      xAxis: {
        data: props.axis,
        boundaryGap: false, //坐标轴两边留白策略
      },
      yAxis: {
        show: false, // 隐藏y轴
      },
      // 定位
      grid: {
        top: 0,
        bottom: 0,
        left: 0,
        right: 0,
      },
      series: [
        {
          type: "line",
          data: props.data,
          // 隐藏拐点
          itemStyle: {
            opacity: 0,
          },
          // 填充区域
          areaStyle: {
            color: "#a64ca6",
            opacity: 1, // 颜色不对是因为默认0.7，改为1
          },
          // 是否平滑曲线显示
          smooth: true,
          // 设置线条颜色和填充颜色一样
          lineStyle: {
            color: "#a64ca6",
          },
        },
      ],
    };
    myEcharts.setOption(option);
  }
);
</script>

<style scoped>
.charts {
  width: 100%;
  height: 100%;
}
</style>
